<template>
	<view>
		<u-navbar
			title="地址管理"
			:autoBack="true"
			placeholder
		>
		</u-navbar>
		<scroll-view :scroll-y="true" :style="{height: scrollViewHeight}">
			<view style="padding: 32rpx;">
				<template v-if="loading">
					<view style="padding: 32rpx;">
						<u-skeleton :avatarSize="0" style="margin-bottom: 32rpx;" :loading="loading" avatar rows="3" v-for="i in 6" :key="i"></u-skeleton>
					</view>
				</template>
				<template v-else-if="!loading && addressList.length>0">
					<u-swipe-action>
				        <u-swipe-action-item
						  @click="deleteAddress(address)"
				          :options="options1"
						  v-for="(address, index) in addressList" 
						  :key="address.id"
						  :index="address.id"
				        >
							<view :class="['address-item', addressList.length -1 === index ? null : 'u-border-bottom']">
								<view class="address-item-l" @click="chooseAddress(address)">
									<view class="address-item-l-t">
										<view style="background-color: #f40215;" class="address-item-l-t-tag" v-if="address.isDefault === '1'">默认</view>
										<view :style="{backgroundColor: appColor}" class="address-item-l-t-tag" v-if="Number(address.tag)>0">{{address.tag_dictText}}</view>
										<view class="address-item-l-t-content">{{address.address}}</view>
									</view>
									<view class="address-item-l-t1">{{address.region + address.address}}</view>
									<view class="address-item-l-t2">
										<view class="address-item-l-t2-1">{{address.name}}</view>
										<view class="address-item-l-t2-2">{{address.phone}}</view>
									</view>
								</view>
								<view class="address-item-r" @click.stop="editAddress(address)">
									<u-icon name="edit-pen"></u-icon>
								</view>
							</view>
				        </u-swipe-action-item>
				    </u-swipe-action>
				</template>
				<template v-else-if="!loading && addressList.length === 0">
					<u-empty
					    mode="data"
						icon="http://cdn.uviewui.com/uview/empty/data.png"
					>
					</u-empty>
				</template>
			</view>
		</scroll-view>
		<view class="good-detail-bootom" v-if="!loading">
			<view style="margin: 0 16rpx;height: 100%;width: calc(100vw - 32rpx);display: flex;align-items: center;">
				<view class="good-detail-bootom-r">
					<u-button @click="add" :color="appColor" text="＋新建收货地址" shape="circle"></u-button>
				</view>
			</view>	
		</view>
	</view>
</template>

<script>
	import {mapActions} from 'vuex';
	import {memberAddress, deleteAddress} from '@/api/me.js';
	export default {
		data() {
			return {
				choose: false,
				loading: true,
				addressList: [],
				options1: [
					{
						text: '删除',
						style: {
							backgroundColor: '#f40215',
						}
					}
				]
			}
		},
		computed: {
			appColor() {
				return this.appPrimaryColor
			},
			scrollViewHeight() {
				return this.scrollHeight - 44 - 50 + 'px';
			}
		},
		methods: {
			...mapActions(['setCurAddress']),
			// 选择收货地址
			chooseAddress(address) {
				if(this.choose) {
					this.setCurAddress({curAddress: address});
					uni.navigateBack({
						delta: 1
					})
				}
			},
			// 查询收货地址
			list() {
				this.loading = true;
				memberAddress().then(res => {
					this.addressList = res.result.records
				}).finally(() => {
					this.loading = false;
				})
			},
			// 新增收货地址
			add() {
				let url = '/pages/me/addAddress'
				this.$cuNavigateTo(url)
			},
			// 编辑收货地址
			editAddress({id}) {
				let url = '/pages/me/addAddress?id=' + id
				this.$cuNavigateTo(url)
			},
			// 删除收货地址
			deleteAddress({id}) {
				const that = this;
				uni.showModal({
					title: '提示',
					content: '是否删除该地址?',
					confirmColor: that.appColor,
					success: function (res) {
						if (res.confirm) {
							deleteAddress(id).then(res => {
								const {success} = res;
								if(success) {
									that.$cuTip('删除成功').then(() => {
										that.list();
									});
								}
							})
						}
					}
				})
			}
		},
		onShow() {
			this.list();
		},
		onLoad({choose}) {
			if(choose) {
				this.choose = true;
			} else {
				this.choose = false;
			}
		}
	}
</script>

<style>
	page {
		background-color: #fff;
	}
</style>
<style lang="scss" scoped>
	.address-item {
		padding: 32rpx 0;
		display: flex;
		.address-item-l {
			flex: 1;
			.address-item-l-t {
				display: flex;
				padding-bottom: 8rpx;
				.address-item-l-t-tag {
					font-size: 24rpx;
					color: #ffffff;
					border-radius: 8rpx;
					padding: 0 4rpx;
					margin-right: 8rpx;
				}
				.address-item-l-t-content {
					font-size: 24rpx;
					color: #666;
				}
			}
			.address-item-l-t1 {
				font-size: 28rpx;
				line-height: 36rpx;
				color: #333;
				font-weight: bold;
			}
			.address-item-l-t2 {
				display: flex;
				align-items: center;
				font-size: 24rpx;
				color: #666;
				padding-top: 4rpx;
				.address-item-l-t2-1 {
					width: 200rpx;
					white-space: nowrap;
					text-overflow: ellipsis;
					overflow: hidden;
				}
				.address-item-l-t2-2 {
					flex: 1;
					padding-left: 32rpx;
				}
			}
		}
		.address-item-r {
			width: 60rpx;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}
	
	.good-detail-bootom {
		background-color: #ffffff;
		position: fixed;
		height: 50px;
		width: 100vw;
		bottom: 0;
		left: 0;
		padding-bottom: constant(safe-area-inset-bottom);     /*兼容 iOS<11.2*/
		padding-bottom: env(safe-area-inset-bottom);  /*兼容 iOS>11.2*/
		box-sizing: content-box;
		.good-detail-bootom-r {
			flex: 1;
			text-align: right
		}
	}
</style>